package com.bxd.baodan.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.fragment.BaseFragment;
import com.bxd.baodan.fragment.ProductCollectFragment;
import com.bxd.baodan.list.XTangCollectFragment;
import com.bxd.baodan.widget.PagerSlidingTabStrip;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class MyFavorateActivity extends BaseActivity {
    private ViewPager pager;
    private List<BaseFragment> list = new ArrayList<BaseFragment>();
    private FragmentPagerAdapter mAdapter;
    PagerSlidingTabStrip tabs;
    final String[] titles = {"  产品  ", "  文章  "};



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorate);
        setActionBarTitle("我的收藏");

        context = this;

        initView();


    }


    private void initView() {
        list.add(new ProductCollectFragment());
        list.add(new XTangCollectFragment());


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return list.get(arg0);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        };

        pager = (ViewPager) findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.strip);
        pager.setAdapter(mAdapter);
        tabs.setViewPager(pager);

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_wdsc_cp");
                        break;
                    case 1:
                        MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_wdsc_wz");
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        setTabsValue();
        //设置小红点,item从0开始计算
        //        tabs.setMsgToast(1, true);

    }


    private void setTabsValue() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        // 设置Tab底部选中的指示器Indicator的高度
        tabs.setIndicatorHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, dm));
        // 设置Tab底部选中的指示器 Indicator的颜色
        tabs.setIndicatorColorResource(R.color.green_lab);
        //设置指示器Indicatorin是否跟文本一样宽，默认false
        tabs.setIndicatorinFollowerTv(true);
        //设置小红点提示，item从0开始计算，true为显示，false为隐藏
        //        tabs.setMsgToast(2, true);
        //设置红点滑动到当前页面自动消失,默认为true
        tabs.setMsgToastPager(true);
        //设置Tab标题文字的颜色
        tabs.setTextColor(getResources().getColor(R.color.black));
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, dm));
        // 设置选中的Tab文字的颜色
        tabs.setSelectedTextColorResource(R.color.green_lab);
        //设置Tab底部分割线的高度
        tabs.setUnderlineHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, dm));
        //设置Tab底部分割线的颜色
        tabs.setUnderlineColorResource(R.color.transparent);
        // 设置点击某个Tab时的背景色,设置为0时取消背景色tabs.setTabBackground(0);
        //        tabs.setTabBackground(R.drawable.bg_tab);
        tabs.setTabBackground(0);
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);

        tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zgw_zy_zxfw");
                } else if (position == 1) {
                    MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zgw_zy_yhpj");
                } else if (position == 2) {
                    MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zgw_zy_cptj");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
